#include "page4.h"

static lv_ft_info_t info1;
static lv_style_t style1;
static lv_obj_t * win1;
static lv_obj_t * ta;
static lv_obj_t * kb;
static const char * btns[] = {""};
static lv_obj_t * msgbox;
static lv_obj_t * list1;

static void show_list1(lv_obj_t * parent);
static void btn_cb1(lv_event_t * e);
static void ta_cb(lv_event_t * e);
static void yes_cb(lv_event_t * e);
static void no_cb(lv_event_t * e);
static void add_cb(lv_event_t * e);
static void sub_cb(lv_event_t * e);
static void select_cb(lv_event_t * e);

void btn_cb1(lv_event_t * e)
{
    page1_show();
    lv_obj_del(page4);
}

void ta_cb(lv_event_t * e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); // 取消隐藏
    lv_keyboard_set_textarea(kb, ta);
}

// 隐藏键盘
void blank_cb2(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
}

void yes_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    plist * c = (plist *)e->user_data;

    const char * ta_text = lv_textarea_get_text(ta);

    int count = atoi(ta_text ? ta_text : "0"); // 文本为空时按0处理
    if(count == 0) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请输入正确的数字", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
    } else if(count > c->count) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "购物车商品数目不足", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
    } else {
        if(list_change(c->name, count, proinf) == 0) {
            c->count -= count;
            c->subtotal -= count * c->price;
            lv_obj_del(list1);
            lv_obj_del(kb);
            show_list1(page4);
            kb = lv_keyboard_create(page4);
            lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
            lv_obj_set_size(kb, 800, 180);
        }
        if(c->count == 0) {
            plist_delete(c->name, purinf);
            lv_obj_del(list1);
            lv_obj_del(kb);
            show_list1(page4);
            kb = lv_keyboard_create(page4);
            lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
            lv_obj_set_size(kb, 800, 180);
        }
    }
    plist_pri(purinf);
}

void no_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);    // 隐藏对象
    if(win1 != NULL && lv_obj_is_valid(win1)) { // 检查win是否存在且有效
        lv_obj_del(win1);                       // 销毁旧弹窗
        win1 = NULL;                            // 重置全局变量
    }
}

void add_cb(lv_event_t * e)
{
    plist * a = (plist *)e->user_data;
    char temp[10];
    int count;
    strcpy(temp, lv_textarea_get_text(ta));
    count = atoi(temp);
    if(count < a->count) {
        count++;
        snprintf(temp, sizeof(temp), "%d", count);
        lv_textarea_set_text(ta, temp);
    }
}

void sub_cb(lv_event_t * e)
{
    plist * a = (plist *)e->user_data;
    char temp[10];
    int count;
    strcpy(temp, lv_textarea_get_text(ta));
    count = atoi(temp);
    if(count > a->count) {
        snprintf(temp, sizeof(temp), "%d", a->count);
        lv_textarea_set_text(ta, temp);
    } else if(count > 0) {
        count--;
        snprintf(temp, sizeof(temp), "%d", count);
        lv_textarea_set_text(ta, temp);
    }
}

void select_cb(lv_event_t * e)
{
    plist * f = purinf;
    while(f->next != purinf) {
        f = f->next;
        if(f->btn == e->target) break;
    }

    if(win1 != NULL && lv_obj_is_valid(win1)) { // 检查win是否存在且有效
        lv_obj_del(win1);                       // 销毁旧弹窗
        win1 = NULL;                            // 重置全局变量
    }

    win1 = lv_obj_create(page4);
    lv_obj_set_size(win1, 440, 280);
    lv_obj_set_pos(win1, 180, 20);

    char buf1[30]; // 名称
    char buf2[30]; // 单价
    char buf3[30]; // 数量
    char buf4[30]; // 小计
    sprintf(buf1, "名称：%s", f->name);
    sprintf(buf2, "单价：%.2f", f->price);
    sprintf(buf3, "数量：%d", f->count);
    sprintf(buf4, "小计：%.2f", f->subtotal);

    // 显示名称
    show_label(win1, 180, 40, 20, 20, &style1, buf1);

    // 显示价格
    show_label(win1, 180, 40, 240, 20, &style1, buf2);

    // 显示库存
    show_label(win1, 180, 40, 20, 80, &style1, buf3);

    // 显示小计
    show_label(win1, 180, 40, 240, 80, &style1, buf4);

    // 创建文本编辑框用于输入购买数量
    ta = lv_textarea_create(win1);
    lv_textarea_add_char(ta, '0');
    lv_textarea_set_accepted_chars(ta, "0123456789");
    lv_obj_set_size(ta, 100, 50);
    lv_obj_set_pos(ta, 170, 140);
    lv_obj_add_event_cb(ta, ta_cb, LV_EVENT_CLICKED, NULL);

    // 减少按键
    lv_obj_t * sub = lv_btn_create(win1);
    lv_obj_set_size(sub, 40, 40);
    lv_obj_set_pos(sub, 100, 140);
    lv_obj_t * lsub = lv_label_create(sub);
    lv_label_set_text(lsub, LV_SYMBOL_MINUS);
    lv_obj_center(lsub);
    lv_obj_add_event_cb(sub, sub_cb, LV_EVENT_CLICKED, f);

    // 增加按键
    lv_obj_t * add = lv_btn_create(win1);
    lv_obj_set_size(add, 40, 40);
    lv_obj_set_pos(add, 300, 140);
    lv_obj_t * ladd = lv_label_create(add);
    lv_obj_center(ladd);
    lv_label_set_text(ladd, LV_SYMBOL_PLUS);
    lv_obj_add_event_cb(add, add_cb, LV_EVENT_CLICKED, f);

    // 确认按键
    lv_obj_t * yes = show_btn_label(win1, 50, 40, 130, 200, &style1, "删除");
    lv_obj_add_event_cb(yes, yes_cb, LV_EVENT_CLICKED, f);

    // 取消按键
    lv_obj_t * no = show_btn_label(win1, 50, 40, 230, 200, &style1, "取消");
    lv_obj_add_event_cb(no, no_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_add_event_cb(win1, blank_cb2, LV_EVENT_CLICKED, NULL);
}

void show_list1(lv_obj_t * parent)
{
    list1 = lv_list_create(parent);
    lv_obj_set_size(list1, 650, 440);
    lv_obj_set_pos(list1, 20, 20);
    lv_obj_add_style(list1, &style1, 0);

    plist * d = purinf->next;

    char buf[128] = {0};
    while(d != purinf) {
        snprintf(buf, sizeof(buf), "商品：%s  单价：%.2f  数量：%d  小计：%.2f", d->name, d->price, d->count,
                 d->subtotal);
        lv_obj_t * btn = lv_list_add_btn(list1, NULL, buf);
        lv_obj_set_size(btn, 600, 60);

        d->btn = btn;
        lv_obj_add_event_cb(btn, select_cb, LV_EVENT_CLICKED, NULL);
        d = d->next;
    }
}

void page4_show()
{
    // 中文字库加载
    /*FreeType uses C standard file system, so no driver letter is required.*/
    info1.name   = "/font/simkai.ttf";
    info1.weight = 24;
    info1.style  = FT_FONT_STYLE_NORMAL;
    info1.mem    = NULL;
    if(!lv_ft_font_init(&info1)) {
        LV_LOG_ERROR("create failed.");
    }
    /*Create style with the new font*/
    lv_style_init(&style1);
    lv_style_set_text_font(&style1, info1.font);
    lv_style_set_text_align(&style1, LV_TEXT_ALIGN_CENTER);

    // 新建页面4
    show_page(&page4);

    // 新建列表
    show_list1(page4);

    // 返回按钮
    lv_obj_t * btn1 = show_btn_label(page4, 80, 80, 700, 20, &style1, "返回");
    lv_obj_add_event_cb(btn1, btn_cb1, LV_EVENT_CLICKED, NULL);

    // 结账按钮
    // lv_obj_t * btn2 = lv_btn_create(page4);
    // lv_obj_set_size(btn2, 80, 80);
    // lv_obj_set_pos(btn2, 700, 380);
    // lv_obj_t * label2 = lv_label_create(btn2);
    // lv_obj_add_style(label2, &style1, 0);
    // lv_label_set_text(label2, "结账");
    // lv_obj_center(label2);
    // lv_obj_add_event_cb(btn2, pay, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(page4);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    lv_obj_set_size(kb, 800, 180);
    lv_obj_add_event_cb(page4, blank_cb2, LV_EVENT_CLICKED, NULL);
}